package com.lc.zpyh.ui.activity.mine;

import android.view.KeyEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.toast.ToastUtils;
import com.lc.widget.view.CountdownView;
import com.lc.zpyh.R;
import com.lc.zpyh.aop.SingleClick;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.request.PhoneSwapeApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.SendSmsApi;
import com.lc.zpyh.http.response.PhoneSwapeBean;
import com.lc.zpyh.manager.InputTextManager;

import okhttp3.Call;

public class ChangePhoneActivity extends AppActivity implements TextView.OnEditorActionListener {
    private EditText mPhoneView;
    private EditText mCodeView;
    private EditText mPhoneNewView;
    private CountdownView mCountdownView;
    private Button mCommitView;

    /** 验证码 */
    private String mVerifyCode;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_changephone;
    }

    @Override
    protected void initView() {
        mPhoneView = findViewById(R.id.et_phone_reset_phone);
        mPhoneNewView = findViewById(R.id.et_phone_new_phone);
        mCodeView = findViewById(R.id.et_phone_reset_code);
        mCountdownView = findViewById(R.id.cv_phone_reset_countdown);
        mCommitView = findViewById(R.id.btn_phone_reset_commit);

        setOnClickListener(mCountdownView, mCommitView);

        mCodeView.setOnEditorActionListener(this);

        InputTextManager.with(this)
                .addView(mPhoneView)
                .addView(mCodeView)
                .addView(mPhoneNewView)
                .setMain(mCommitView)
                .build();
    }

    @Override
    protected void initData() {

    }
    @SingleClick
    @Override
    public void onClick(View view) {
        if (view == mCountdownView) {

            if (mPhoneView.getText().toString().length() != 11) {
                mPhoneView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                toast(R.string.common_phone_input_error);
                return;
            }
            if (mPhoneNewView.getText().toString().length() != 11) {
                mPhoneNewView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                toast(R.string.common_phone_input_error);
                return;
            }



            // 获取验证码
            EasyHttp.post(this)
                    .api(new SendSmsApi()
                            .setPhone(mPhoneNewView.getText().toString())).request(new HttpCallback<PublicMsgBean>(this) {

                @Override
                public void onStart(Call call) {
                    mCountdownView.start();
                }

                @Override
                public void onEnd(Call call) {

                }

                @Override
                public void onSucceed(PublicMsgBean data) {
                    toast(data.getList());
                }

                @Override
                public void onFail(Exception e) {
                    super.onFail(e);

                }
            });

        } else if (view == mCommitView) {

            if (mPhoneView.getText().toString().length() != 11) {
                mPhoneView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                toast(R.string.common_phone_input_error);
                return;
            }
            if (mPhoneNewView.getText().toString().length() != 11) {
                mPhoneNewView.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.shake_anim));
                toast(R.string.common_phone_input_error);
                return;
            }
            if (mPhoneNewView.getText().toString().trim().equals(mPhoneView.getText().toString().trim())) {

                toast("手机号不可一致");
                return;
            }
            if (mCodeView.getText().toString().length() != 6) {
                ToastUtils.show(R.string.common_code_error_hint);
                return;
            }

            // 隐藏软键盘
            hideKeyboard(getCurrentFocus());


            // 更换手机号
            phoneSwap();

        }
    }

    private void phoneSwap() {
        EasyHttp.post(this)
                .api(new PhoneSwapeApi()
                        .setOldPhone(mPhoneView.getText().toString()).setCode(mCodeView.getText().toString()).setNewPhone(mPhoneNewView.getText().toString())).request(new HttpCallback<PhoneSwapeBean>(this) {


            @Override
            public void onSucceed(PhoneSwapeBean data) {
                if (data.getCode() == 0) {

                        toast("更换成功");
                        finish();


                }else {
                    toast(data.getList());
                }

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        return false;
    }
}
